package com.example.music_liwei.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.music_liwei.R;

public class FirstActivity extends AppCompatActivity {

    private static final String PREFS_NAME = "MyPrefs";
    private static final String KEY_AGREEMENT_ACCEPTED = "agreement_accepted";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        boolean agreementAccepted = preferences.getBoolean(KEY_AGREEMENT_ACCEPTED, false);

        if (!agreementAccepted) {
            showAgreementDialog();
        } else {
            // 用户已同意，1秒后跳转到主页面
            new Handler().postDelayed(() -> {
                Intent intent = new Intent(FirstActivity.this, MusicMainActivity.class);
                startActivity(intent);
                finish();
            }, 1000);
        }
    }

    private void showAgreementDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_agreement, null);
        builder.setView(dialogView);

        TextView messageTextView = dialogView.findViewById(R.id.tv_message);

        String message = "欢迎使用音乐社区，我们将严格遵守相关法律和隐私政策保护您的个人隐私，请您阅读并同意《用户协议》与《隐私政策》。";
        SpannableString spannableString = new SpannableString(message);

        // 设置《用户协议》为可点击链接
        ClickableSpan userAgreementSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                // 处理用户协议点击事件
                Toast.makeText(FirstActivity.this, "点击了用户协议", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE); // 设置文本颜色
                ds.setUnderlineText(false); // 去掉下划线
            }
        };

        // 设置《隐私政策》为可点击链接
        ClickableSpan privacyPolicySpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                // 处理隐私政策点击事件
                Toast.makeText(FirstActivity.this, "点击了隐私政策", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE); // 设置文本颜色
                ds.setUnderlineText(false); // 去掉下划线
            }
        };

        // 添加可点击链接到SpannableString
        spannableString.setSpan(userAgreementSpan, message.indexOf("《用户协议》"), message.indexOf("《用户协议》") + "《用户协议》".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(privacyPolicySpan, message.indexOf("《隐私政策》"), message.indexOf("《隐私政策》") + "《隐私政策》".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        messageTextView.setText(spannableString);
        messageTextView.setMovementMethod(LinkMovementMethod.getInstance());

        builder.setCancelable(false);

        AlertDialog dialog = builder.create();

        dialogView.findViewById(R.id.btn_decline).setOnClickListener(v -> {
            // 处理不同意按钮点击事件
            dialog.dismiss();
            finish();
        });

        dialogView.findViewById(R.id.btn_accept).setOnClickListener(v -> {
            // 处理同意按钮点击事件
            SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(KEY_AGREEMENT_ACCEPTED, true);
            editor.apply();

            dialog.dismiss();
            Intent intent = new Intent(FirstActivity.this, MusicMainActivity.class);
            startActivity(intent);
            finish();
        });

        dialog.show();
    }
}
